package north

import (
	"iot-base/common/config"
	"iot-base/common/mqtt"
	"iot-base/common/notify"
	"iot-base/common/types"
	"strconv"
	"strings"
)

func handleAction(client mqtt.IOTMQTTBroker, topic string, pCmd *mqtt.Cmd) {
	if pCmd != nil {
		if data, ok := pCmd.Body.(*types.ReqActionData); ok {
			// 发布主题消息
			notify.PubMsg(false, "SendActionCmd", data)
			var buf strings.Builder
			buf.WriteString("edge_SendActionCmd.")
			buf.WriteString(data.Action)
			buf.WriteString(strconv.FormatInt(data.Seq, 10))
			buf.WriteString(".msg.list")

			var replyData types.RespRecordData
			err := notify.GetResult(buf.String(), &replyData, 3)
			if err != nil {
				replyData.ID = data.Action
				replyData.Seq = data.Seq
				replyData.Code = -1
				replyData.Msg = "time out"
				client.Reply(config.GetCloudTopic(), pCmd, 0, ""+pCmd.SourceID, replyData)
				return
			}
			client.Reply(config.GetCloudTopic(), pCmd, 0, ""+pCmd.SourceID, replyData)
			return
		}
	} else {
		client.Reply(config.GetCloudTopic(), pCmd, -1, "not exist "+pCmd.SourceID, nil)
		return
	}

}
